package com.rocky.ssm.controller;

import com.rocky.ssm.model.Talk;
import com.rocky.ssm.service.ICommunication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */

@Controller
@RequestMapping("/students")
public class CommunicationController {

    @Autowired
    private ICommunication communication;

    @RequestMapping("/communicationList")
    public String list(Model model){
        List<Talk> list =communication.getTalkList();
        model.addAttribute("list",list);
        return "communication/list";
    }

    @RequestMapping("/{id}/communicationUpdate")
    public String update(@PathVariable int id, Model model){
        Talk talk=communication.getTalkById(id);
        model.addAttribute("talk",talk);
        return "communication/update";
    }

    @RequestMapping(value = "/doCommunicationUpdate",method = RequestMethod.POST)
    public String doUpdate(Talk talk){
        if(communication.update(talk)>0)
            return "redirect:/students/communicationList";
        else
            return "communication/update";
    }

    @RequestMapping("/{id}/communicationDel")
    public String delete(@PathVariable int id){
        communication.delete(id);
        return "redirect:/students/communicationList";
    }

    @RequestMapping("/skipCommunicationAdd")
    public String skipCommunicationAdd(){
        return "communication/add";
    }

    @RequestMapping(value = "/communicationAdd",method = RequestMethod.POST)
    public String add(Talk talk){
        if(communication.save(talk)>0)
            return "redirect:/students/communicationList";
        else
            return "communication/add";
    }



}
